from typing import Any

from numba import jit
import numpy as np
import Geometry
import time
from .Log import logger
from functools import partial, wraps
from Constants import CHUNK_SIZE, CHUNK_HEIGHT

GlobalDefaults = Any


class JitFunc:

    def get_func_run_timed(self):
        """用于测试jit函数加载的时间"""

        @wraps(self)
        def func_run_inner(*args, **kwargs):
            logger.info(f"开始编译{self.__name__}函数...")
            start_time = time.time()
            result = self(*args, **kwargs)
            stop_time = time.time()
            logger.info(f"编译{self.__name__}函数用时:{stop_time - start_time:.2f}s")
            return result

        return func_run_inner

    get_func_run_timed = staticmethod(get_func_run_timed)

    @classmethod
    def init_jit_func(cls):
        """jit函数初始化"""
        logger.info("初始化jit函数...")

        # 设置jit函数的默认值，函数中设置可变默认值会无法缓存编译后的jit函数
        jit_funcs = {
            cls.generate_cloud_mesh_jit: {  # 函数的默认值
                "face_vertices": Geometry.cloud_use_face_vertices,
                "clouds_layout": Geometry.clouds_layout
            }
        }

        for func_name, func_dict in jit_funcs.items():
            partial_func_name = partial(func_name, **func_dict)
            setattr(cls, func_name.__name__, partial_func_name)

        # 初始化函数
        logger.info(f"开始编译jit函数...")
        start_time = time.time()

        cls.generate_cloud_mesh_jit(np.array([0.0, 0.0]), 3)
        cls.generate_data_jit()

        logger.info(f"编译jit函数总用时:{time.time() - start_time:.2f}s")

    @staticmethod
    @jit(nopython=True, fastmath=True, cache=True)
    def generate_cloud_mesh_jit(center_pos, radius, face_vertices: GlobalDefaults, clouds_layout: GlobalDefaults):
        min_x, min_y, max_x, max_y = int(center_pos[0] - radius), int(center_pos[1] - radius), int(
            center_pos[0] + radius), int(center_pos[1] + radius)
        max_vertices = (radius * 2 - 1) * (radius * 2 - 1) * 6 * 4
        vertices = np.empty(max_vertices * 10, dtype=np.float32)
        indices = np.empty(max_vertices * 6, dtype=np.uint32)
        vertex_count = 0
        index_count = 0

        # vertices, indices = [], []
        index = 0
        for x in range(min_x, max_x):
            for y in range(min_y, max_y):
                l = clouds_layout[x & 0xff, y & 0xff]
                if l == 0:
                    continue
                else:
                    dx, dy = x - center_pos[0], y - center_pos[1]
                    start = 4 * 4
                    vertices[vertex_count:vertex_count + 4 * 6] = [
                        face_vertices[start][0] + dx,
                        face_vertices[start][1],
                        face_vertices[start][2] + dy,
                        face_vertices[start][3],
                        face_vertices[start][4],
                        face_vertices[start][5],

                        face_vertices[start + 1][0] + dx,
                        face_vertices[start + 1][1],
                        face_vertices[start + 1][2] + dy,
                        face_vertices[start + 1][3],
                        face_vertices[start + 1][4],
                        face_vertices[start + 1][5],

                        face_vertices[start + 2][0] + dx,
                        face_vertices[start + 2][1],
                        face_vertices[start + 2][2] + dy,
                        face_vertices[start + 2][3],
                        face_vertices[start + 2][4],
                        face_vertices[start + 2][5],

                        face_vertices[start + 3][0] + dx,
                        face_vertices[start + 3][1],
                        face_vertices[start + 3][2] + dy,
                        face_vertices[start + 3][3],
                        face_vertices[start + 3][4],
                        face_vertices[start + 3][5]
                    ]
                    vertex_count += 4 * 6
                    indices[index_count:index_count + 6] = [index, index + 1, index + 2, index, index + 2, index + 3]
                    index_count += 6
                    index += 4

                    start = 4 * 5
                    vertices[vertex_count:vertex_count + 4 * 6] = [
                        face_vertices[start][0] + dx,
                        face_vertices[start][1],
                        face_vertices[start][2] + dy,
                        face_vertices[start][3],
                        face_vertices[start][4],
                        face_vertices[start][5],

                        face_vertices[start + 1][0] + dx,
                        face_vertices[start + 1][1],
                        face_vertices[start + 1][2] + dy,
                        face_vertices[start + 1][3],
                        face_vertices[start + 1][4],
                        face_vertices[start + 1][5],

                        face_vertices[start + 2][0] + dx,
                        face_vertices[start + 2][1],
                        face_vertices[start + 2][2] + dy,
                        face_vertices[start + 2][3],
                        face_vertices[start + 2][4],
                        face_vertices[start + 2][5],

                        face_vertices[start + 3][0] + dx,
                        face_vertices[start + 3][1],
                        face_vertices[start + 3][2] + dy,
                        face_vertices[start + 3][3],
                        face_vertices[start + 3][4],
                        face_vertices[start + 3][5]
                    ]
                    vertex_count += 4 * 6
                    indices[index_count:index_count + 6] = [index, index + 1, index + 2, index, index + 2, index + 3]
                    index_count += 6
                    index += 4

                    ll = clouds_layout[(x - 1) & 0xff, y & 0xff]
                    if ll == 0:
                        start = 4 * 2
                        vertices[vertex_count:vertex_count + 4 * 6] = [
                            face_vertices[start][0] + dx,
                            face_vertices[start][1],
                            face_vertices[start][2] + dy,
                            face_vertices[start][3],
                            face_vertices[start][4],
                            face_vertices[start][5],

                            face_vertices[start + 1][0] + dx,
                            face_vertices[start + 1][1],
                            face_vertices[start + 1][2] + dy,
                            face_vertices[start + 1][3],
                            face_vertices[start + 1][4],
                            face_vertices[start + 1][5],

                            face_vertices[start + 2][0] + dx,
                            face_vertices[start + 2][1],
                            face_vertices[start + 2][2] + dy,
                            face_vertices[start + 2][3],
                            face_vertices[start + 2][4],
                            face_vertices[start + 2][5],

                            face_vertices[start + 3][0] + dx,
                            face_vertices[start + 3][1],
                            face_vertices[start + 3][2] + dy,
                            face_vertices[start + 3][3],
                            face_vertices[start + 3][4],
                            face_vertices[start + 3][5]
                        ]
                        vertex_count += 4 * 6
                        indices[index_count:index_count + 6] = [index, index + 1, index + 2, index, index + 2,
                                                                index + 3]
                        index_count += 6
                        index += 4
                    lr = clouds_layout[(x + 1) & 0xff, y & 0xff]
                    if lr == 0:
                        start = 4 * 3
                        vertices[vertex_count:vertex_count + 4 * 6] = [
                            face_vertices[start][0] + dx,
                            face_vertices[start][1],
                            face_vertices[start][2] + dy,
                            face_vertices[start][3],
                            face_vertices[start][4],
                            face_vertices[start][5],

                            face_vertices[start + 1][0] + dx,
                            face_vertices[start + 1][1],
                            face_vertices[start + 1][2] + dy,
                            face_vertices[start + 1][3],
                            face_vertices[start + 1][4],
                            face_vertices[start + 1][5],

                            face_vertices[start + 2][0] + dx,
                            face_vertices[start + 2][1],
                            face_vertices[start + 2][2] + dy,
                            face_vertices[start + 2][3],
                            face_vertices[start + 2][4],
                            face_vertices[start + 2][5],

                            face_vertices[start + 3][0] + dx,
                            face_vertices[start + 3][1],
                            face_vertices[start + 3][2] + dy,
                            face_vertices[start + 3][3],
                            face_vertices[start + 3][4],
                            face_vertices[start + 3][5]
                        ]
                        vertex_count += 4 * 6
                        indices[index_count:index_count + 6] = [index, index + 1, index + 2, index, index + 2,
                                                                index + 3]
                        index_count += 6
                        index += 4
                    lu = clouds_layout[x & 0xff, (y + 1) & 0xff]
                    if lu == 0:
                        start = 4 * 0
                        vertices[vertex_count:vertex_count + 4 * 6] = [
                            face_vertices[start][0] + dx,
                            face_vertices[start][1],
                            face_vertices[start][2] + dy,
                            face_vertices[start][3],
                            face_vertices[start][4],
                            face_vertices[start][5],

                            face_vertices[start + 1][0] + dx,
                            face_vertices[start + 1][1],
                            face_vertices[start + 1][2] + dy,
                            face_vertices[start + 1][3],
                            face_vertices[start + 1][4],
                            face_vertices[start + 1][5],

                            face_vertices[start + 2][0] + dx,
                            face_vertices[start + 2][1],
                            face_vertices[start + 2][2] + dy,
                            face_vertices[start + 2][3],
                            face_vertices[start + 2][4],
                            face_vertices[start + 2][5],

                            face_vertices[start + 3][0] + dx,
                            face_vertices[start + 3][1],
                            face_vertices[start + 3][2] + dy,
                            face_vertices[start + 3][3],
                            face_vertices[start + 3][4],
                            face_vertices[start + 3][5]
                        ]
                        vertex_count += 4 * 6
                        indices[index_count:index_count + 6] = [index, index + 1, index + 2, index, index + 2,
                                                                index + 3]
                        index_count += 6
                        index += 4
                    ld = clouds_layout[x & 0xff, (y - 1) & 0xff]
                    if ld == 0:
                        start = 4 * 1
                        vertices[vertex_count:vertex_count + 4 * 6] = [
                            face_vertices[start][0] + dx,
                            face_vertices[start][1],
                            face_vertices[start][2] + dy,
                            face_vertices[start][3],
                            face_vertices[start][4],
                            face_vertices[start][5],

                            face_vertices[start + 1][0] + dx,
                            face_vertices[start + 1][1],
                            face_vertices[start + 1][2] + dy,
                            face_vertices[start + 1][3],
                            face_vertices[start + 1][4],
                            face_vertices[start + 1][5],

                            face_vertices[start + 2][0] + dx,
                            face_vertices[start + 2][1],
                            face_vertices[start + 2][2] + dy,
                            face_vertices[start + 2][3],
                            face_vertices[start + 2][4],
                            face_vertices[start + 2][5],

                            face_vertices[start + 3][0] + dx,
                            face_vertices[start + 3][1],
                            face_vertices[start + 3][2] + dy,
                            face_vertices[start + 3][3],
                            face_vertices[start + 3][4],
                            face_vertices[start + 3][5]
                        ]
                        vertex_count += 4 * 6
                        indices[index_count:index_count + 6] = [index, index + 1, index + 2, index, index + 2,
                                                                index + 3]
                        index_count += 6
                        index += 4
        return vertices[:vertex_count], indices[:index_count]

    @staticmethod
    @jit(nopython=True, fastmath=True, cache=True)
    def generate_data_jit(blocks=None,
                          forward_blocks=None,
                          backward_blocks=None,
                          left_blocks=None,
                          right_blocks=None,
                          forward_left_blocks=None,
                          forward_right_blocks=None,
                          backward_left_blocks=None,
                          backward_right_blocks=None,
                          block_texture_id=None,
                          block_transparent=None,
                          block_smoothness=None,
                          block_shape=None):
        if blocks is None:
            print("编译完成！")
            return None
        max_vertices = CHUNK_SIZE * CHUNK_HEIGHT * CHUNK_SIZE * 6 * 4

        verticesn = Geometry.vertices
        directionsn = Geometry.directions
        plant_verticesn = Geometry.plant_vertices
        padsn = Geometry.pads
        verticesj = np.empty(max_vertices * 10, dtype=np.float32)
        indicesj = np.empty(max_vertices * 6, dtype=np.uint32)  # 每个面6个索引

        index = 0
        vertex_count = 0
        index_count = 0

        for x in range(0, CHUNK_SIZE):
            for z in range(0, CHUNK_SIZE):
                for y in range(0, CHUNK_HEIGHT):
                    block_type = blocks[x, y, z]
                    if block_type == 0:
                        continue
                    shape = block_shape[block_type]
                    if shape == 1:  # 花卉
                        v_add = plant_verticesn
                        smoothness = block_smoothness[block_type]
                        face_uv_coord = block_texture_id[block_type][0]
                        # 替换 verticesj.extend() 为:
                        verticesj[vertex_count:vertex_count + 8 * 10] = [
                            v_add[0][0] + x, v_add[0][1] + y, v_add[0][2] + z, face_uv_coord[0][0],
                            face_uv_coord[0][1], v_add[0][5], v_add[0][6], v_add[0][7], 0.0, smoothness,
                            v_add[1][0] + x, v_add[1][1] + y, v_add[1][2] + z, face_uv_coord[1][0],
                            face_uv_coord[1][1], v_add[1][5], v_add[1][6], v_add[1][7], 0.0, smoothness,
                            v_add[2][0] + x, v_add[2][1] + y, v_add[2][2] + z, face_uv_coord[2][0],
                            face_uv_coord[2][1], v_add[2][5], v_add[2][6], v_add[2][7], 0.0, smoothness,
                            v_add[3][0] + x, v_add[3][1] + y, v_add[3][2] + z, face_uv_coord[3][0],
                            face_uv_coord[3][1], v_add[3][5], v_add[3][6], v_add[3][7], 0.0, smoothness,

                            v_add[4][0] + x, v_add[4][1] + y, v_add[4][2] + z, face_uv_coord[0][0],
                            face_uv_coord[0][1], v_add[4][5], v_add[4][6], v_add[4][7], 0.0, smoothness,
                            v_add[5][0] + x, v_add[5][1] + y, v_add[5][2] + z, face_uv_coord[1][0],
                            face_uv_coord[1][1], v_add[5][5], v_add[5][6], v_add[5][7], 0.0, smoothness,
                            v_add[6][0] + x, v_add[6][1] + y, v_add[6][2] + z, face_uv_coord[2][0],
                            face_uv_coord[2][1], v_add[6][5], v_add[6][6], v_add[6][7], 0.0, smoothness,
                            v_add[7][0] + x, v_add[7][1] + y, v_add[7][2] + z, face_uv_coord[3][0],
                            face_uv_coord[3][1], v_add[7][5], v_add[7][6], v_add[7][7], 0.0, smoothness,
                        ]
                        vertex_count += 8 * 10
                        indicesj[index_count: index_count + 24] = [
                            index, index + 1, index + 2, index + 2, index + 3, index + 0,
                                   index + 2, index + 1, index, index, index + 3, index + 2,
                                   index + 4, index + 5, index + 6, index + 6, index + 7, index + 4,
                                   index + 6, index + 5, index + 4, index + 4, index + 7, index + 6
                        ]
                        index_count += 24
                        index += 8
                        continue
                    for d in range(0, 6):  # 检查六面是否为固体，若为固体则不渲染

                        fx, fy, fz = x + directionsn[d][0], y + directionsn[d][1], z + directionsn[d][2]

                        if 0 <= fx < CHUNK_SIZE and 0 <= fy < CHUNK_HEIGHT and 0 <= fz < CHUNK_SIZE:
                            neighbor_block_type = blocks[fx, fy, fz]
                        elif fy == -1 or fy == CHUNK_HEIGHT:
                            neighbor_block_type = 1
                        elif fx == -1 and 0 <= fz < CHUNK_SIZE:
                            if left_blocks is None:
                                neighbor_block_type = 1
                            neighbor_block_type = left_blocks[fy, fz]
                        elif fx == CHUNK_SIZE and 0 <= fz < CHUNK_SIZE:
                            if right_blocks is None:
                                neighbor_block_type = 1
                            neighbor_block_type = right_blocks[fy, fz]
                        elif 0 <= fx < CHUNK_SIZE and fz == -1:
                            if backward_blocks is None:
                                neighbor_block_type = 1
                            neighbor_block_type = backward_blocks[fx, fy]
                        elif 0 <= fx < CHUNK_SIZE and fz == CHUNK_SIZE:
                            if forward_blocks is None:
                                neighbor_block_type = 1
                            neighbor_block_type = forward_blocks[fx, fy]
                        elif fx == CHUNK_SIZE and fz == CHUNK_SIZE:
                            if forward_right_blocks is None:
                                neighbor_block_type = 1
                            neighbor_block_type = forward_right_blocks[fy]
                        elif fx == -1 and fz == CHUNK_SIZE:
                            if forward_left_blocks is None:
                                neighbor_block_type = 1
                            neighbor_block_type = forward_left_blocks[fy]
                        elif fx == CHUNK_SIZE and fz == -1:
                            if backward_right_blocks is None:
                                neighbor_block_type = 1
                            neighbor_block_type = backward_right_blocks[fy]
                        elif fx == -1 and fz == -1:
                            if backward_left_blocks is None:
                                neighbor_block_type = 1
                            neighbor_block_type = backward_left_blocks[fy]
                        else:
                            neighbor_block_type = 1

                        if neighbor_block_type != -1 and block_transparent[neighbor_block_type]:  # 此面为透明，则绘制此面
                            face_uv_coord = block_texture_id[block_type][d]
                            v_start = 4 * d
                            v_add = verticesn[v_start:v_start + 4]

                            p_start = v_start * 4
                            p_add = padsn[p_start:p_start + 16]

                            v_ao = np.array([0, 0, 0, 0], dtype=np.float32)

                            smoothness = block_smoothness[block_type]

                            for i in range(0, 4):
                                res = 3
                                up_index = 4 * i
                                corner_index = up_index + 1
                                side1_index = corner_index + 1
                                side2_index = side1_index + 1

                                fx, fy, fz = x + p_add[up_index][0], y + p_add[up_index][1], z + p_add[up_index][2]
                                if 0 <= fx < CHUNK_SIZE and 0 <= fy < CHUNK_HEIGHT and 0 <= fz < CHUNK_SIZE:
                                    up_block = blocks[fx, fy, fz]
                                elif fy == -1 or fy == CHUNK_HEIGHT:
                                    up_block = 1
                                elif fx == -1 and 0 <= fz < CHUNK_SIZE:
                                    if left_blocks is None:
                                        up_block = 1
                                    up_block = left_blocks[fy, fz]
                                elif fx == CHUNK_SIZE and 0 <= fz < CHUNK_SIZE:
                                    if right_blocks is None:
                                        up_block = 1
                                    up_block = right_blocks[fy, fz]
                                elif 0 <= fx < CHUNK_SIZE and fz == -1:
                                    if backward_blocks is None:
                                        up_block = 1
                                    up_block = backward_blocks[fx, fy]
                                elif 0 <= fx < CHUNK_SIZE and fz == CHUNK_SIZE:
                                    if forward_blocks is None:
                                        up_block = 1
                                    up_block = forward_blocks[fx, fy]
                                elif fx == CHUNK_SIZE and fz == CHUNK_SIZE:
                                    if forward_right_blocks is None:
                                        up_block = 1
                                    up_block = forward_right_blocks[fy]
                                elif fx == -1 and fz == CHUNK_SIZE:
                                    if forward_left_blocks is None:
                                        up_block = 1
                                    up_block = forward_left_blocks[fy]
                                elif fx == CHUNK_SIZE and fz == -1:
                                    if backward_right_blocks is None:
                                        up_block = 1
                                    up_block = backward_right_blocks[fy]
                                elif fx == -1 and fz == -1:
                                    if backward_left_blocks is None:
                                        up_block = 1
                                    up_block = backward_left_blocks[fy]
                                else:
                                    up_block = 1

                                if not block_transparent[up_block]:
                                    v_ao[i] = 0
                                    continue

                                fx, fy, fz = x + p_add[corner_index][0], y + p_add[corner_index][1], z + \
                                             p_add[corner_index][2]
                                if 0 <= fx < CHUNK_SIZE and 0 <= fy < CHUNK_HEIGHT and 0 <= fz < CHUNK_SIZE:
                                    corner_block = blocks[fx, fy, fz]
                                elif fy == -1 or fy == CHUNK_HEIGHT:
                                    corner_block = 1
                                elif fx == -1 and 0 <= fz < CHUNK_SIZE:
                                    if left_blocks is None:
                                        corner_block = 1
                                    corner_block = left_blocks[fy, fz]
                                elif fx == CHUNK_SIZE and 0 <= fz < CHUNK_SIZE:
                                    if right_blocks is None:
                                        corner_block = 1
                                    corner_block = right_blocks[fy, fz]
                                elif 0 <= fx < CHUNK_SIZE and fz == -1:
                                    if backward_blocks is None:
                                        corner_block = 1
                                    corner_block = backward_blocks[fx, fy]
                                elif 0 <= fx < CHUNK_SIZE and fz == CHUNK_SIZE:
                                    if forward_blocks is None:
                                        corner_block = 1
                                    corner_block = forward_blocks[fx, fy]
                                elif fx == CHUNK_SIZE and fz == CHUNK_SIZE:
                                    if forward_right_blocks is None:
                                        corner_block = 1
                                    corner_block = forward_right_blocks[fy]
                                elif fx == -1 and fz == CHUNK_SIZE:
                                    if forward_left_blocks is None:
                                        corner_block = 1
                                    corner_block = forward_left_blocks[fy]
                                elif fx == CHUNK_SIZE and fz == -1:
                                    if backward_right_blocks is None:
                                        corner_block = 1
                                    corner_block = backward_right_blocks[fy]
                                elif fx == -1 and fz == -1:
                                    if backward_left_blocks is None:
                                        corner_block = 1
                                    corner_block = backward_left_blocks[fy]
                                else:
                                    corner_block = 1

                                fx, fy, fz = x + p_add[side1_index][0], y + p_add[side1_index][1], z + \
                                             p_add[side1_index][2]
                                if 0 <= fx < CHUNK_SIZE and 0 <= fy < CHUNK_HEIGHT and 0 <= fz < CHUNK_SIZE:
                                    side1_block = blocks[fx, fy, fz]
                                elif fy == -1 or fy == CHUNK_HEIGHT:
                                    side1_block = 1
                                elif fx == -1 and 0 <= fz < CHUNK_SIZE:
                                    if left_blocks is None:
                                        side1_block = 1
                                    side1_block = left_blocks[fy, fz]
                                elif fx == CHUNK_SIZE and 0 <= fz < CHUNK_SIZE:
                                    if right_blocks is None:
                                        side1_block = 1
                                    side1_block = right_blocks[fy, fz]
                                elif 0 <= fx < CHUNK_SIZE and fz == -1:
                                    if backward_blocks is None:
                                        side1_block = 1
                                    side1_block = backward_blocks[fx, fy]
                                elif 0 <= fx < CHUNK_SIZE and fz == CHUNK_SIZE:
                                    if forward_blocks is None:
                                        side1_block = 1
                                    side1_block = forward_blocks[fx, fy]
                                elif fx == CHUNK_SIZE and fz == CHUNK_SIZE:
                                    if forward_right_blocks is None:
                                        side1_block = 1
                                    side1_block = forward_right_blocks[fy]
                                elif fx == -1 and fz == CHUNK_SIZE:
                                    if forward_left_blocks is None:
                                        side1_block = 1
                                    side1_block = forward_left_blocks[fy]
                                elif fx == CHUNK_SIZE and fz == -1:
                                    if backward_right_blocks is None:
                                        side1_block = 1
                                    side1_block = backward_right_blocks[fy]
                                elif fx == -1 and fz == -1:
                                    if backward_left_blocks is None:
                                        side1_block = 1
                                    side1_block = backward_left_blocks[fy]
                                else:
                                    side1_block = 1

                                fx, fy, fz = x + p_add[side2_index][0], y + p_add[side2_index][1], z + \
                                             p_add[side2_index][2]
                                if 0 <= fx < CHUNK_SIZE and 0 <= fy < CHUNK_HEIGHT and 0 <= fz < CHUNK_SIZE:
                                    side2_block = blocks[fx, fy, fz]
                                elif fy == -1 or fy == CHUNK_HEIGHT:
                                    side2_block = 1
                                elif fx == -1 and 0 <= fz < CHUNK_SIZE:
                                    if left_blocks is None:
                                        side2_block = 1
                                    side2_block = left_blocks[fy, fz]
                                elif fx == CHUNK_SIZE and 0 <= fz < CHUNK_SIZE:
                                    if right_blocks is None:
                                        side2_block = 1
                                    side2_block = right_blocks[fy, fz]
                                elif 0 <= fx < CHUNK_SIZE and fz == -1:
                                    if backward_blocks is None:
                                        side2_block = 1
                                    side2_block = backward_blocks[fx, fy]
                                elif 0 <= fx < CHUNK_SIZE and fz == CHUNK_SIZE:
                                    if forward_blocks is None:
                                        side2_block = 1
                                    side2_block = forward_blocks[fx, fy]
                                elif fx == CHUNK_SIZE and fz == CHUNK_SIZE:
                                    if forward_right_blocks is None:
                                        side2_block = 1
                                    side2_block = forward_right_blocks[fy]
                                elif fx == -1 and fz == CHUNK_SIZE:
                                    if forward_left_blocks is None:
                                        side2_block = 1
                                    side2_block = forward_left_blocks[fy]
                                elif fx == CHUNK_SIZE and fz == -1:
                                    if backward_right_blocks is None:
                                        side2_block = 1
                                    side2_block = backward_right_blocks[fy]
                                elif fx == -1 and fz == -1:
                                    if backward_left_blocks is None:
                                        side2_block = 1
                                    side2_block = backward_left_blocks[fy]
                                else:
                                    side2_block = 1

                                if not block_transparent[side1_block] and not block_transparent[side2_block]:
                                    v_ao[i] = 1 - Geometry.v_ao_colors[4]
                                    continue
                                if not block_transparent[corner_block]:
                                    res -= 1
                                if not block_transparent[side1_block]:
                                    res -= 1
                                if not block_transparent[side2_block]:
                                    res -= 1
                                v_ao[i] = 1 - Geometry.v_ao_colors[res]

                            # 替换 verticesj.extend() 为:
                            verticesj[vertex_count:vertex_count + 4 * 10] = [
                                v_add[0][0] + x, v_add[0][1] + y, v_add[0][2] + z, face_uv_coord[0][0],
                                face_uv_coord[0][1], v_add[0][5], v_add[0][6], v_add[0][7], v_ao[0], smoothness,
                                v_add[1][0] + x, v_add[1][1] + y, v_add[1][2] + z, face_uv_coord[1][0],
                                face_uv_coord[1][1], v_add[1][5], v_add[1][6], v_add[1][7], v_ao[1], smoothness,
                                v_add[2][0] + x, v_add[2][1] + y, v_add[2][2] + z, face_uv_coord[2][0],
                                face_uv_coord[2][1], v_add[2][5], v_add[2][6], v_add[2][7], v_ao[2], smoothness,
                                v_add[3][0] + x, v_add[3][1] + y, v_add[3][2] + z, face_uv_coord[3][0],
                                face_uv_coord[3][1], v_add[3][5], v_add[3][6], v_add[3][7], v_ao[3], smoothness
                            ]
                            vertex_count += 4 * 10
                            if v_ao[0] + v_ao[2] > v_ao[1] + v_ao[3]:
                                indicesj[index_count: index_count + 6] = [
                                    index, index + 1, index + 3, index + 1, index + 2, index + 3
                                ]
                            else:
                                indicesj[index_count: index_count + 6] = [
                                    index, index + 1, index + 2, index + 2, index + 3, index
                                ]
                            index_count += 6
                            index += 4
        return verticesj[:vertex_count], indicesj[:index_count]
